var app = getApp();
var ApiUrl = app.globalData.ApiUrl;
var HTTP = app.globalData.HTTP;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    scrollTop: 0
  },
  onLoad: function (options) {  
      var that = this;
      var key = wx.getStorageSync('key');
      var curpage = 1;
      var store_id = options.store_id;
     
      //加载店铺详情
      wx.request({
        url: ApiUrl + "/index.php?act=store&op=store_info",
        data: { key: key, store_id: store_id },
        dataType: 'json',
        success: function (result) {        
          //banner 背景图
          var data = result.data.datas;   
          if (!data.store_info.mb_title_img) {
            var topBgs = [];
            var WapSiteUrl = HTTP;
            var randomBgIndex = "";
            topBgs[0] = WapSiteUrl + "/images/store_h_bg_01.jpg";
            topBgs[1] = WapSiteUrl + "/images/store_h_bg_02.jpg";
            topBgs[2] = WapSiteUrl + "/images/store_h_bg_03.jpg";
            topBgs[3] = WapSiteUrl + "/images/store_h_bg_04.jpg";
            topBgs[4] = WapSiteUrl + "/images/store_h_bg_05.jpg";
            randomBgIndex = Math.round(Math.random() * 4);           
            data.store_info.mb_title_img = topBgs[randomBgIndex];           
          }  
          
          that.setData({
            ngp: "",
            goodsPg: "",
            allGoods:"none",
            homePage:'block',          
            newgoods:"none",
            showmore: "hide",
            store_id: store_id,
            curpage: curpage,
            homePg: "selected", 
            selected:"selected",
            unselected:"",
            store_info: data.store_info,
            rec_goods_list: data.rec_goods_list,
            storeactivity: "none",
            agp: ""
          }); 
          that.goodsRank();
      }
    });   
},
//商品排行 
goodsRank:function(option){
  var that = this; 
  var store_id = that.data.store_id;
  var type = "collectdesc";

  if (option){
    type = option.target.dataset.type+'desc';
  }  
  if (type =="collectdesc"){
   var selected="selected";
   var unselected = "";
  }else{
    var selected = "collectdesc";
   var unselected = "selected";
  }
    //商品排行 
    wx.request({
      url: ApiUrl + '/index.php?act=store&op=store_goods_rank',
      data: { store_id: store_id, ordertype:type, num: "3" },
      dataType: 'json',
      success: function (result) {
          
        if (result.data.code == 200) {
          var  goodsList = result.data.datas.goods_list;
           
          that.setData({
            goodsList:goodsList,
            ordertype: type,
            unselected: unselected,
            selected: selected
          }); 
          
        }
      }
  });  
},
storeactivity:function(option){
  var that = this;
  var store_id = option.target.dataset.store_id;
   
  wx.request({
    url: ApiUrl + '/index.php?act=store&op=store_promotion',
    data: { store_id: store_id},    
    dataType: 'json',     
    success: function(res) {
      
      that.setData({
        storeActivity:res.data.datas.promotion,
        newgoods: "none",
        allGoods: "none",
        homePage: "none",
        storeactivity:"",
        homePg: "",
        ngp: "",
        goodsPg: "",
        agp:"selected"
      });
    },
    fail: function(res) {},
    complete: function(res) {},
  })

}, 
 //全部商品
changeGoods:function(options){
      var person = '';
      var sale = '';
      var ural = '';
      var param = {};
      var that = this;
      var order_key = that.data.order_key;
      var order_val = that.data.order_val;
      var listBefore = new Array();


      wx.getSystemInfo({
        success: function (res) {
          that.setData({
            scrollHeight: res.windowHeight,    
            goodsPg: "selected",
            homePg:"",
            ngp: "",
            storeactivity: "none",
            agp: ""
           });
        }
      });     
      
      if (that.data.goods_list) {
        listBefore = that.data.goods_list;
      }

      switch (order_key){
          case "5":
               person = 'current';
               break;
          case "3":    
              sale = 'current';
              break;
          default :           
              ural = 'current';
              break;               
      }
      
      param.store_id = that.data.store_id;
      param.key = order_key ? order_key:"0";
      param.order = order_val?order_val:"0";      
      param.page = 10;
      param.curpage = that.data.curpage;
      wx.request({
        url: ApiUrl + '/index.php?act=store&op=store_goods',
        data: param,
        dataType: 'json',
        success: function (res) {
            
          var data = res.data.datas; 
          that.setData({
            sale: sale, 
            ural: ural,
            person: person,
            homePage: "none",
            allGoods: "block",
            hasmore: res.data.hasmore,
            goods_list: listBefore.concat(data.goods_list),
          });          
          that.data.curpage++;
        },        
      });         
  },
  loadGoods: function () {
    this.changeGoods();
  },
  //加载商品上新
  getStoreNewGoods:function(){
    var param = {};
    var that = this;
    param.store_id = that.data.store_id;
    wx.request({
      url: ApiUrl + '/index.php?act=store&op=store_new_goods',
      data: param,
      dataType: "json",
      success: function(res) {
          var data = res.data.datas;
          that.setData({
            newgoods_list: data.goods_list,
            hasmore: data.hasmore,
            newgoods: "",
            allGoods:"none",
            homePage: "none",
            homePg: "",
            ngp:"selected",
            goodsPg:"",
             storeactivity: "none",
            agp: ""
          });         
      },      
    });
 
  },
  
  // 销量优先排序
  sort_salesnum:function(options){

     var that = this;    
     var opt = new Array();
     var data = options.currentTarget.dataset;
     that.setData({
       order_key: data.order_key,
       order_val: data.order_val,
       curpage: 1,
     });  
     that.data.goods_list = new Array();  
     that.changeGoods();
     
  },
   //收藏店铺
  collect: function (options) {
      var that = this;
      var key = wx.getStorageSync('key');  
      var store_id = that.data.store_id;
      
      if (!key) {        
        wx.redirectTo({
          url: '/pages/login/login'
        })        
      }

      if (store_id <= 0) {        
        wx.showToast({
          title: '参数错误',
          icon: 'fail',
          duration: 2000
        });      
        return false;
      }

      wx.request({         
        url: ApiUrl + '/index.php?act=member_favorites_store&op=favorites_add',
        data: {"key":key,"store_id":store_id},
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        method: 'post',
        success:function(res){
            if(res.data.code==200){
              wx.showToast({
                title: '收藏成功',
                icon: 'success',
                duration: 2000
              });   
              var store_info = that.data.store_info;
              store_info.is_favorate = "true";
              store_info.store_collect = parseInt(store_info.store_collect)+1;
              that.setData({
                store_info: store_info
              });              
            }
        }
      })
   
  },
  dropFavoriteStore:function(){

    var that = this;
    var store_id = that.data.store_id;
    var key = wx.getStorageSync('key');
    var store_info = "";
    if (!key) {
      wx.redirectTo({
        url: '/pages/login/login',
      }) 
    }

    if (store_id <= 0) {
      wx.showModal({
        title: '',
        content: '参数错误',
      }) 
    }else{
        wx.request({
          url: ApiUrl + '/index.php?act=member_favorites_store&op=favorites_del',
          data: { key: key, store_id: store_id },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          method: 'post',
          success: function (res) {   
             
            if(res.data.code==200){
                wx.showToast({
                  title: '取消成功',
                  icon: 'success',
                  duration: 2000
                });
                store_info = that.data.store_info; 
                store_info.is_favorate = false;
                store_info.store_collect = parseInt(store_info.store_collect)-1;

                that.setData({
                  store_info: store_info
                });                
            }            
          }
        })
    }
    
  },
  imageLoad: function (val) {
    var type = val.target.dataset.type;
    var imgH = val.detail.height;
    var imgW = val.detail.width;
    var screeW = wx.getSystemInfoSync().windowWidth;
    var screeR = screeW / imgW;
    var height = imgH * screeR + "px";
    var that = this;
 
    switch (type) {
      case "home1pic":
        that.setData({
          home1H: height,
        });
        break;
      case "advPic":
        that.setData({
          advPicH: height
        }); break;
      case "recommend":
        that.setData({
          goodsH: imgW / 2
        }); break;

    }

  },
})